#include <bits/stdc++.h>
using namespace std;
/**
 * 双端队列放数据
 * 向量容器存放顺序 直接output即可
 * */

int main() {
    int n;
    cin >> n;
    deque<string> boy;
    deque<string> girl;
    vector<int> sort;
    for (int i = 1; i <= n; i++) {
        int sex;
        string name;
        cin >> sex >> name;
        if (i <= n / 2) sort.push_back(sex);
        if (sex == 1) boy.push_back(name);
        else if (sex == 0) girl.push_back(name);
    }

    for (int i = 0; i < n / 2; i++) {
        if (sort[i]) {
            cout << boy.front() << " " << girl.back() << endl;
            boy.pop_front();
            girl.pop_back();
        } else {
            cout << girl.front() << " " << boy.back() << endl;
            girl.pop_front();
            boy.pop_back();
        }
    }

    return 0;
}